<?php
/*
 * Aclass_cg 导航类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com
 */
class Aclass_cg{
	function  __construct() {
	}
	/*
	function cg($cgname){
		//检查菜单是否已生成
		$Acg = Afun_cache_get($cgname);
		if(empty($Acg)){
			Afun_cg_create($cgname);
		}
		return ;
	}*/

/*读取单个cg并缓存*/
	function cg_one($key){
		$apps = Afun_cache_get('acg_one_'.$key);
		if(empty($apps)){
			$model=Afun_db()->query("SELECT * FROM `".Acan_table_cg."` WHERE name = '$key' or id = '$key'")->fetch();//获取app菜单
			$apps = $this->cg_list_child($model['id'],'0');
			Afun_cache_set('acg_'.$key,$apps);//写入缓存
		}
		return $apps;
	}
	
/*读取所有cg并缓存*/
	function cg_list(){
		$apps = Afun_cache_get('acg_all');
		if(empty($apps)){
			$cgrs=Afun_db()->query("SELECT * FROM `".Acan_table_cg."` WHERE pid='1' ORDER BY `look` ASC")->fetchAll();//获取app菜单
			foreach($cgrs as $a=>$val){
				if($val['if_use']=='1'){
					$vali['id'] = $val['id'];
					$vali['value'] = $val['value'];
					$vali['name'] = $val['name'];
					$vali['cgname'] = $val['cgname'];
					$vali['_cg'] = $this->cg_list_child($val['id']);
					$apps->$val['name'] = $vali;
				}
			}
			Afun_cache_set('acg_all',$apps);//写入缓存
		}
		return $apps;
	}
	
	//获取下级菜单 {不存在则返回'0'}
	function cg_list_child($pid,$if_child='1'){
		$listpid = Afun_db()->query("SELECT * FROM `a_cg` WHERE pid='$pid' ORDER BY `look` ASC")->fetchAll();//获取module菜单
		if($listpid){
			foreach ($listpid as $k=>$val){
				if($val['if_use']=='1'){
					$vali['id'] = $val['id'];
					$vali['value'] = $val['value'];
					$vali['name'] = $val['name'];
					$vali['cgname'] = $val['cgname'];
					if($if_child=='1')
						$vali['_cg'] = $this->cg_list_child($val['id']);
					$apps[$val['name']] = $vali;
				}
			}
			return $apps;
		}else{
			return false;
		}
	}

//--更新菜单(不存在则新加)
	function cg_create(){
		$Aclass_file = Afunc_file();
		$cg_pdir = $Aclass_file->ascandiro(A_ROOT.'/app/module/')->dir;//读取app name
		foreach ($cg_pdir as $p=>$app){
			if($app == '.svn'){ continue; }//屏蔽不正常的文件夹
			$pid0 = $this->cg_create_sql($app,'0','1');//存取app name的ID(不存在则插入,存在则取出ID)
			$cg_dir = $Aclass_file->ascandiro(A_ROOT.'/app/module/'.$app.'/')->dir;//读取app下的module name
			foreach ($cg_dir as $k=>$module){
				if($module == '.svn'){ continue; }//屏蔽不正常的文件夹
				$pid1 = $this->cg_create_sql($module,$pid0,'2');//存取module name的ID(不存在则插入,存在则取出ID)
				$str = $Aclass_file->ascandiro(A_ROOT.'/app/module/'.$app.'/'.$module.'/')->file;//获取module下的act
				foreach ($str as $i=>$act){
					$act = str_replace('.php', '', $act);
					$this->cg_create_sql($act,$pid1,'3');//存取act name(不存在则插入,存在则取出ID)
				}
			}
		}
		return $cg_dir;
	}

	/*
	 * 创建菜单
	 * 新纪录则返回 最后插入的ID
	 * 存在纪录则返回 自己的ID
	 */
	function cg_create_sql($aurl,$pid,$class){
		$cgcount = Afun_db()->query("SELECT count(*) as count,id FROM `a_cg` WHERE `pid`='$pid' and `class`='$class' and `aurl`='$aurl'")->fetch();
		if($cgcount['count']=='0'){
			Afun_db()->exec("INSERT INTO `a_cg` (`pid`,`aurl`,`class`,`name`) VALUES ('$pid','$aurl','$class','$aurl')");
			return Afun_db()->lastInsertId();
		}
		return $cgcount['id'];
	}
	/*
	 * 检查是否有子菜单
	 * 存在返回 1
	 * 不存在返回 2
	 */
	function cg_check_child($id){
		$cgcount = Afun_db()->query("SELECT count(*) as count FROM `a_cg` WHERE `pid`='$id'")->fetch();
		if($cgcount['count']>'0'){
			if($cgcount['if_child']!='1'){
				Afun_db()->exec("UPDATE `a_cg` SET `if_child`='1' WHERE `if_child`='$id'");
			}
			return 1;
		}else{
			if($cgcount['if_child']!='2'){
				Afun_db()->exec("UPDATE `a_cg` SET `if_child`='2' WHERE `if_child`='$id'");
			}
			return 2;
		}
	}


}

?>
